<?php

namespace app\admin\controller;
use app\admin\model\Goods as GoodsModel;

class Goods extends Common
{
    public function index() {
        return $this->fetch();
    }

    public function goods_edit () {
        $goods_id = input('get.goods_id','','intval');
        if(!$goods_id){
            $this->error('缺少参数');
        }
        $sql = GoodsModel::where('goods_id',$goods_id)->find();
        if(!$sql){
            $this->error('找不到商品');
        }
        $this->assign('goods',$sql);
        return $this->fetch();
    }

    public function edit () {
        $goods_id = input('post.goods_id','','intval');
        $goods_name = input('post.goods_name','','trim');
        $goods_simple_name = input('post.goods_simple_name','','trim');
        $goods_quotation = input('post.goods_quotation','','trim');

        if(!$goods_id){
            return $this->returnApiData(self::CODE_COMMON_ERROR, [], '商品ID错误');
        }
        if(empty($goods_name)){
            return $this->returnApiData(self::CODE_COMMON_ERROR, [], '商品名称不能为空');
        }
        if(empty($goods_simple_name)){
            return $this->returnApiData(self::CODE_COMMON_ERROR, [], '名称简写不能为空');
        }
        try{
            $goods = GoodsModel::update([
                'goods_name' => $goods_name,
                'goods_simple_name' => $goods_simple_name,
                'goods_quotation' => $goods_quotation,
            ],['goods_id'=>$goods_id]);
            return $this->returnApiData(self::CODE_COMMON_SUCCESS, $goods , '更新成功');
        }catch(\Exception $e) {
            return $this->returnApiData(self::CODE_COMMON_ERROR, [], '更新失败');
        }
    }

    public function getList() {
        $currPage = input('get.currPage','1','intval');
        $goods_name = input('get.goods_name','','trim');
        $goods_sn = input('get.goods_sn','','trim');
        $goods_code = input('get.goods_code','','trim');
        $goods_simple_name = input('get.goods_simple_name','','trim');

        $returnArr = ['result'=>true,
            'msg'=>'',
            'pagination'=>[
                'currPage' => $currPage,
                'total' => 0,
                'pageSize' => self::PAGE_SIZE
            ],
            'datas'=>[]
            ];

        $res = GoodsModel::where('goods_name','like','%'.$goods_name.'%')
            ->where('goods_sn','like','%'.$goods_sn.'%')
            ->where('goods_code','like','%'.$goods_code.'%')
            ->where('goods_simple_name','like','%'.$goods_simple_name.'%')
            ->limit(($currPage-1)*$returnArr['pagination']['pageSize'], $returnArr['pagination']['pageSize'])
            ->order('goods_id','desc')->select();

//        echo db('goods')->getLastSql();

        $total = GoodsModel::where('goods_name','like','%'.$goods_name.'%')
            ->where('goods_sn','like','%'.$goods_sn.'%')
            ->where('goods_code','like','%'.$goods_code.'%')
            ->where('goods_simple_name','like','%'.$goods_simple_name.'%')
            ->field("count('goods_id') as allnum")
            ->find();


        $returnArr['pagination']['total'] = $total['allnum'];
        $returnArr['datas'] = $res;

        return $this->returnApiData(self::CODE_COMMON_SUCCESS, $returnArr , '');
    }
    public function goods_add (){
        return $this->fetch();
    }
    public function add (){
        $goods_name = input('post.goods_name','','trim');
        $goods_sn = input('post.goods_sn','','trim');
        $goods_code = input('post.goods_code','','trim');
        $goods_simple_name = input('post.goods_simple_name','','trim');
        $goods_quotation = input('post.goods_quotation','','trim');

        if(empty($goods_name)){
            return $this->returnApiData(self::CODE_COMMON_ERROR, [], '商品名称不能为空');
        }
        if(empty($goods_sn)){
            return $this->returnApiData(self::CODE_COMMON_ERROR, [], '货号不能为空');
        }
        if(empty($goods_code)){
            return $this->returnApiData(self::CODE_COMMON_ERROR, [], '条形码不能为空');
        }
        if(empty($goods_simple_name)){
            return $this->returnApiData(self::CODE_COMMON_ERROR, [], '名称简写不能为空');
        }
        try{
            $goods = GoodsModel::create([
                'goods_name' => $goods_name,
                'goods_sn' => $goods_sn,
                'goods_code' => $goods_code,
                'goods_simple_name' => $goods_simple_name,
                'goods_quotation' => $goods_quotation,
            ]);

            return $this->returnApiData(self::CODE_COMMON_SUCCESS, $goods , '添加成功');
        }catch(\Exception $e) {
            return $this->returnApiData(self::CODE_COMMON_ERROR, [], '添加失败');
        }
    }
}